/* 
 * Copyright (C)2008 Vladimir Hozjan vladimir.hozjan@gmail.com
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
 */

/**
 * @file tetrisMatrix.cpp  
 *
 * Implementation of tetris matrix class 
 *
 * @author Vladimir Hozjan
 * @date   28.12.2008 
 *
 * @remarks VHOZ [28.12.2008]: file created
 */
#include "tetrisMatrix.h"

/**
 * TetrisMatrix
 *
 * Constructor for matrix. Requires number of rows and columns.
 *
 * @param[in] aNumRows - number of rows
 * @param[in] aNumCols - number of columns
 */
TetrisMatrix::TetrisMatrix( unsigned short aNumRows, unsigned short aNumCols ) : 
mNumRows(0),
mNumCols(0),
mMatrix(0)
{
  initialize(aNumRows, aNumCols);
}

/**
 * TetrisMatrix
 *
 * Copy constructor
 *
 * @param[in] TetrisMatrix
 *
 */
TetrisMatrix::TetrisMatrix( TetrisMatrix const & aMatrix) : 
mNumRows(0),
mNumCols(0),
mMatrix(0)
{
  initialize(aMatrix.mNumRows, aMatrix.mNumCols);
}

/**
 * ~TetrisMatrix
 *
 * Destructor
 */
TetrisMatrix::~TetrisMatrix() {
  /*Free all memory*/
  unsigned short i;
  for (i = 0; i < mNumRows; i ++) {
    delete [] mMatrix[i];
  }
  delete [] mMatrix;
}

/**
 * getCell
 *
 * Return value in matrix, that is on position aRow, aCol. If cell 
 * doesn't exists method return 0;
 *
 * @param[in] int aRow
 * @param[in] int aCol
 *
 * @return Value of cell in matrix. 
 */
unsigned short TetrisMatrix::getCell( unsigned short aRow, unsigned short aCol ) const {
  unsigned short value = 0;
  if ((aRow < mNumRows) && (aCol < mNumCols)) {
    value = mMatrix[aRow][aCol];
  }
  return value;
}

/**
 * initialize
 *
 * Initialization of matrix
 *
 * @param[in] unsigned short aNumRows
 * @param[in] unsigned short aNumCols
 */
void TetrisMatrix::initialize( unsigned short aNumRows, unsigned short aNumCols ) {
  mMatrix = new unsigned short*[aNumRows];
  if (mMatrix != 0) {
    unsigned short i,j;
    for (i = 0; i < aNumRows; i ++) {
      unsigned short *tmp = new unsigned short[aNumCols];
      if (tmp != 0) {
        mMatrix[i] = tmp;
        for (j = 0; j < aNumCols; j++) {
          mMatrix[i][j] = 0;
        }
      } else {
        /*if error*/
        delete[] mMatrix;
        mMatrix = 0;
        return;
      }
    }

    /*All elements allocated properly - set member variables*/
    mNumCols = aNumCols;
    mNumRows = aNumRows;
  }
}